package com.car.loan.project.cm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.controller.BaseController;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmDealer;
import com.car.loan.project.cm.domain.CmDealerAccount;
import com.car.loan.project.cm.service.ICmDealerAccountService;
import com.car.loan.project.cm.service.ICmDealerService;
import com.car.loan.project.cm.util.bo.CmDealerAccountBo;
import com.car.loan.project.cm.util.bo.CmDealerAccountUpdateBo;
import com.car.loan.project.cm.util.bo.DealerBaseBo;
import com.car.loan.project.cm.util.enunms.DealerStatus;
import com.car.loan.project.cm.util.select.DealerBaseSelect;
import com.car.loan.project.xr.util.RedisDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/10/21 19:32
 */
@RestController
@RequestMapping("/cm/dealer")
@Api(value = "经销商管理", tags = "基础数据-车辆经销商(合作商户)管理接口")
public class CmDealerController extends BaseController {

    @Autowired
    private ICmDealerService dealerService;

    @Autowired
    private ICmDealerAccountService dealerAccountService;

    @Resource
    private RedisDataUtil redisDataUtil;

    /**
     * 获取经销商列表
     */
    @PreAuthorize("@ss.hasPermi('cm:dealer:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取经销商列表", responseContainer = "list", response = CmDealer.class)
    public AjaxResult list(@Valid DealerBaseSelect dealerBaseSelect) {
        IPage<CmDealer> page = new Page<>(dealerBaseSelect.getPageNum(),
            dealerBaseSelect.getPageSize());
        CmDealer cmDealer = new CmDealer();
        if (null != dealerBaseSelect.getStatus()) {
            cmDealer.setStatus(dealerBaseSelect.getStatus());
        }
        QueryWrapper<CmDealer> queryWrapper = new QueryWrapper<>(cmDealer);
        if (StringUtils.isNotEmpty(dealerBaseSelect.getName())) {
            queryWrapper.like("name", dealerBaseSelect.getName());
        }
        return AjaxResult.success(dealerService.selectPage(page, queryWrapper));
    }


    /**
     * 获取经销商详情
     */
    @PreAuthorize("@ss.hasPermi('cm:dealer:query')")
    @GetMapping(value = "/{dealerId}")
    @ApiOperation(value = "获取经销商详情", response = CmDealer.class)
    public AjaxResult getInfo(@PathVariable Long dealerId) {
        return AjaxResult.success(dealerService.selectCmDealerById(dealerId));
    }


    /**
     * 新增经销商
     */
    @PreAuthorize("@ss.hasPermi('cm:dealer:add')")
    @Log(title = "经销商管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加经销商")
    public AjaxResult add(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name) {
        if (!dealerService.checkDealerNameUnique(name, 0L)) {
            return AjaxResult.error("添加失败，该经销商名称已存在");
        }
        CmDealer cmDealer = new CmDealer();
        cmDealer.setName(name);
        cmDealer.setStatus(DealerStatus.NORMAL.status);
        AjaxResult ajaxResult = toAjax(dealerService.insertCmDealer(cmDealer));
        redisDataUtil.reloadDealerRedis();
        return ajaxResult;
    }

    /**
     * 修改经销商
     */
    @PreAuthorize("@ss.hasPermi('cm:dealer:edit')")
    @Log(title = "经销商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改经销商")
    public AjaxResult edit(@RequestBody @Valid DealerBaseBo dealerBaseBo) {
        if (StringUtils.isNotEmpty(dealerBaseBo.getName())) {
            if (!dealerService
                .checkDealerNameUnique(dealerBaseBo.getName(), dealerBaseBo.getId())) {
                return AjaxResult.error("修改失败，该经销商名称已存在");
            }
        }
        CmDealer cmDealer = new CmDealer();
        cmDealer.setId(dealerBaseBo.getId());
        cmDealer.setName(dealerBaseBo.getName());
        cmDealer.setStatus(dealerBaseBo.getStatus());
        dealerService.updateCmDealer(cmDealer);
        redisDataUtil.reloadDealerRedis();
        return AjaxResult.success();
    }


    @PreAuthorize("@ss.hasPermi('cm:dealer:query')")
    @GetMapping(value = "account/{dealerId}")
    @ApiOperation(value = "获取经销商账户详情", response = CmDealer.class)
    public AjaxResult getInfoAccount(@PathVariable Long dealerId) {
        CmDealerAccount cmDealerAccount = new CmDealerAccount();
        cmDealerAccount.setDealerId(dealerId);
        return AjaxResult.success(dealerAccountService.selectCmDealerAccountList(cmDealerAccount));
    }


    /**
     * 新增经销商
     */
    @PreAuthorize("@ss.hasPermi('cm:dealer:add')")
    @Log(title = "经销商管理", businessType = BusinessType.INSERT)
    @PostMapping("account")
    @ApiOperation("添加经销商账户")
    public AjaxResult addAccount(@RequestBody @Valid CmDealerAccountBo cmDealerAccountBo) {
        CmDealerAccount cmDealerAccount = new CmDealerAccount();
        cmDealerAccount.setDealerId(cmDealerAccountBo.getDealerId());
        cmDealerAccount.setAccountName(cmDealerAccountBo.getAccountName());
        cmDealerAccount.setBankName(cmDealerAccountBo.getBankName());
        cmDealerAccount.setBankAccountNo(cmDealerAccountBo.getBankAccountNo());
        cmDealerAccount.setStatus(DealerStatus.NORMAL.status);
        cmDealerAccount.setCreatorId(SecurityUtils.getLoginUserId());
        return toAjax(dealerAccountService.insertCmDealerAccount(cmDealerAccount));
    }

    /**
     * 修改经销商
     */
    @PreAuthorize("@ss.hasPermi('cm:dealer:edit')")
    @Log(title = "经销商管理", businessType = BusinessType.UPDATE)
    @PutMapping("account")
    @ApiOperation("修改经销商账户")
    public AjaxResult editAccount(
        @RequestBody @Valid CmDealerAccountUpdateBo cmDealerAccountUpdateBo) {
        CmDealerAccount cmDealerAccount = new CmDealerAccount();
        cmDealerAccount.setId(cmDealerAccountUpdateBo.getId());
        cmDealerAccount.setAccountName(cmDealerAccountUpdateBo.getAccountName());
        cmDealerAccount.setBankName(cmDealerAccountUpdateBo.getBankName());
        cmDealerAccount.setBankAccountNo(cmDealerAccountUpdateBo.getBankAccountNo());
        cmDealerAccount.setStatus(cmDealerAccountUpdateBo.getStatus());
        dealerAccountService.updateCmDealerAccount(cmDealerAccount);
        return AjaxResult.success();
    }

}
